#include "gsyncer.h"
#include <stdio.h>
#include <cstdlib>
#include <unistd.h>
using namespace std;

void file_copy(int des, int src)
{
	int res;
	char buf[BUF_SIZ];

	while( (res = readn(src,buf,BUF_SIZ)) > 0 )
	{
		Writen(des,buf,res);
	}

	if( res == 0 )
	{
		return;
	} 
	else
	{
		perror("read");
		exit(-1);
	}
}

//带计数的文件接受例程
void file_rcv(int des, int src)
{
	int res,num;
	char buf[BUF_SIZ];

	while(true)
	{
		res = readn(src,&num,sizeof(int));
		if( res <= 0 )
			break;
		if( num == 0 )    //文件尾
			return;

		res = readn(src,buf,num);
		if(res <= 0)
			break;

		Writen(des,buf,res);
	}

	if( res == 0 )
	{
		perror("连接提前关闭");
		exit(-1);
	} 
	else
	{
		perror("read");
		exit(-1);
	}
}
